package com.xm.xmapp.tool;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.xm.xmapp.app.MyAppcation;

public class MyCache {

	private static MyCache myCache;
	private LruCache<String, String> map;
	private LruCache<String, Bitmap> bitmap;

	public synchronized static MyCache getMyCache() {

		if (myCache == null)
			myCache = new MyCache();
		return myCache;

	}
	public synchronized void reMoveCache(String key){
		
		try {
			map.remove(key);
			ACache.get(MyAppcation.getMyAppcation()).remove(key);
			
		} catch (Exception e) {
			
		}
	}

	public synchronized void putCache(String key, String value) {
		try {

			map.put(key, value);
			ACache.get(MyAppcation.getMyAppcation()).put(key, value);

		} catch (Exception e) {
			
		}
	}

	public synchronized void clearCache() {

		ACache.get(MyAppcation.getMyAppcation()).clear();

	}

	public synchronized String getCache(String key) {
		String str = null;
		try {

			str = map.get(key);
			if (str == null || str.equals(""))
				str = ACache.get(MyAppcation.getMyAppcation()).getAsString(key);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return str;

	}

	public synchronized void putBitmapCache(String key, Bitmap value) {
		try {

			bitmap.put(key, value);
			ACache.get(MyAppcation.getMyAppcation()).put(key, value);

		} catch (Exception e) {

		}
	}

	public synchronized Bitmap getBitmapCache(String key) {
		Bitmap bmp = null;
		try {

			bmp = bitmap.get(key);
			if (bmp == null || bmp.equals(""))
				bmp = ACache.get(MyAppcation.getMyAppcation()).getAsBitmap(key);
		} catch (Exception e) {

		}
		return bmp;

	}

	public synchronized LruCache<String, String> getMap() {
		if (map == null) {
			int mTotalSize = (int) Runtime.getRuntime().totalMemory();
			map = new LruCache<String, String>(mTotalSize / 8) {
			};
		}
		return map;
	}

	public synchronized LruCache<String, Bitmap> getBitMap() {
		if (bitmap == null) {
			int mTotalSize = (int) Runtime.getRuntime().totalMemory();
			bitmap = new LruCache<String, Bitmap>(mTotalSize / 8) {
			};
		}
		return bitmap;
	}

	private MyCache() {
		int mTotalSize = (int) Runtime.getRuntime().totalMemory();
		if (map == null) {
			map = new LruCache<String, String>(mTotalSize / 8) {
			};
		}
		if (bitmap == null) {
			bitmap = new LruCache<String, Bitmap>(mTotalSize / 8);
		}
	}

}
